<template>
  <div v-if="show" class="popup-overlay" @click="handleOverlayClick">
    <div class="popup-content" @click.stop>
      <slot></slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted, onBeforeUnmount, watch } from 'vue'

export default defineComponent({
  name: 'UniPopup',
  props: {
    modelValue: {
      type: Boolean,
      default: false,
    },
    type: {
      type: String,
      default: 'center',
    },
  },
  emits: ['update:modelValue'],
  setup(props, { emit }) {
    const show = ref(props.modelValue)

    watch(
      () => props.modelValue,
      (newVal) => {
        show.value = newVal
      },
    )

    const handleOverlayClick = () => {
      show.value = false
      emit('update:modelValue', false)
    }

    const open = () => {
      show.value = true
      emit('update:modelValue', true)
    }

    const close = () => {
      show.value = false
      emit('update:modelValue', false)
    }

    return {
      show,
      handleOverlayClick,
      open,
      close,
    }
  },
})
</script>

<style scoped>
.popup-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}

.popup-content {
  background: white;
  border-radius: 10px;
  padding: 20px;
  max-width: 90%;
  max-height: 90%;
  overflow-y: auto;
}
</style>
